package jdk8;
import java.util.Arrays;
import java.util.List;

public class StreamDemo02 {
    public static void main(String[] args) {
        List<Integer> list1= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        //1.求集合中奇数的个数
        long count=list1.stream()
                         .filter(x->x%2!=0)
                         .count();
        System.out.println(count);
        //2.求集合中所有偶数的和
        //方案1-sum()
        int sum=list1.stream()
                     .filter(x->x%2==0)
                     .mapToInt(x->x)
                     .sum();
        System.out.println(sum);//30
        //方案2-reduce()缩减操作
        sum=list1.stream()
                 .filter(x->x%2==0)
                 .reduce(0,(x,y)->x+y);//这里的含义是累加操作，0表示初始值
        System.out.println(sum);
    }
}
